package com.midipad.controls;

import com.midipad.R;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;

/**
 * A fader (horizontal or vertical)
 * @author Tim
 * 
 */
public class Fader extends MidiController {
	
	/** distinction between horizontal and vertical faders */
	public enum TYPES {HORIZONTAL, VERTICAL};
	protected TYPES type;
	
	//TODO:private long lastTouchTime = -1;
	
	public Fader(Context context, int x, int y, int width, int height, int[] midiMessages, TYPES type) {
		super(context, 1, "Fader", x, y, width, height, midiMessages);
		this.type = type;
		//set up background
		if(type==TYPES.VERTICAL)
			this.setBackgroundDrawable(getResources().getDrawable(R.drawable.back_slider_9p));
		else
			this.setBackgroundDrawable(getResources().getDrawable(R.drawable.back_sliderh_9p));
		//for correct initial drawing, reinitialize the values of vertical fader
		if(type==TYPES.VERTICAL)
			values[0]=height;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Paint paint = new Paint();
		paint.setStyle(Paint.Style.FILL);
		paint.setColor(Color.WHITE);
		if(type==TYPES.VERTICAL)
			canvas.drawRect(0, values[0], width, height, paint);
		else
			canvas.drawRect(0, 0, values[0], height, paint);
		paint.setColor(Color.RED);
		canvas.drawText(Integer.toString(midiValues[0]), width/2, height/2, paint);
	}

	@Override
	public void updateValues(int x, int y, MotionEvent me) {
		switch(type){
		case VERTICAL:
			values[0] = y;
			midiValues[0] = MAX_MIDI_MSG_VAL-(values[0]*MAX_MIDI_MSG_VAL/height);
			break;
		case HORIZONTAL:
			values[0] = x;
			midiValues[0] = values[0]*MAX_MIDI_MSG_VAL/width;
		}
	}

	@Override
	public void setValues(int val1, int val2, int val3) {
		switch(type){
		case VERTICAL:
			//TODO:verify
			values[0] = val3*height/MAX_MIDI_MSG_VAL;
			midiValues[0] = MAX_MIDI_MSG_VAL-(values[0]*MAX_MIDI_MSG_VAL/height);
			break;
		case HORIZONTAL:
			values[0] = val3*width/MAX_MIDI_MSG_VAL;
			midiValues[0] = values[0]*MAX_MIDI_MSG_VAL/width;
		}
	}
}
